package com.project.frba.bifront.server.rpc;

import java.util.List;

import org.springframework.context.ApplicationContext;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.project.frba.bi.BiUtil;
import com.project.frba.bi.dashboard.DashboardDAO;
import com.project.frba.bi.dashboard.DashboardQueryBuilder;
import com.project.frba.bifront.client.model.Dashboard;
import com.project.frba.bifront.client.rpc.DashboardServices;

public class DashboardServicesImpl extends RemoteServiceServlet implements DashboardServices {

	public Boolean addGraphicToDashboard(String codedInfo, Long dashboardId, Integer position, Integer dashboardType) {
		String query;
		if(dashboardType.equals(Dashboard.DUAL)){
			query = DashboardQueryBuilder.updateDualDashboardFieldQuery(Dashboard.getField(position));
		} else {
			query = DashboardQueryBuilder.updateQuadDashboardFieldQuery(Dashboard.getField(position));
		}
		
		ApplicationContext context = BiUtil.getApplicationContext();
		DashboardDAO dao = (DashboardDAO)context.getBean("dashboardDAO");
		Object[] params = new Object[]{
				codedInfo,
				dashboardId
		};
		
		dao.updateDashboard(query, params);
		
		return true;
	}

	public Boolean createDashboard(Dashboard dashboard) {
		String userId = this.getThreadLocalRequest().getSession().getAttribute("userId").toString();
		String query;
		
		if(dashboard.getType().equals(Dashboard.DUAL)){
			query = DashboardQueryBuilder.insertDualDashboardQuery();		
		} else {
			query = DashboardQueryBuilder.insertQuadDashboardQuery();
		}
		
		ApplicationContext context = BiUtil.getApplicationContext();
		DashboardDAO dao = (DashboardDAO)context.getBean("dashboardDAO");
		
		Integer id = dao.createDashoard(query, new Long(userId), dashboard.getName());
		if(id > 0)
			return true;
		
		return false;
	}

	public Boolean removeDashboard(Long dashboardId, Integer type) {
		ApplicationContext context = BiUtil.getApplicationContext();
		DashboardDAO dao = (DashboardDAO)context.getBean("dashboardDAO");
		
		String query;
		if(Dashboard.DUAL.equals(type)){
			query = DashboardQueryBuilder.removeDualDashboardQuery();
		} else {
			query = DashboardQueryBuilder.removeQuadDashboardQuery();
		}
		dao.removeDashboard(query, dashboardId);
		
		return true;
	}
	
	public Boolean removeDashboard(Dashboard dashboard) {
		return removeDashboard(dashboard.getId(), dashboard.getType());
	}

	public Boolean removeGraphicToDashboard(Long dashboardId, Integer position, Integer dashboardType) {
		return addGraphicToDashboard(null, dashboardId, position, dashboardType);
	}

	public List<Dashboard> getMyDashboards() {
		String userId = this.getThreadLocalRequest().getSession().getAttribute("userId").toString();
		
		ApplicationContext context = BiUtil.getApplicationContext();
		DashboardDAO dao = (DashboardDAO)context.getBean("dashboardDAO");
		
		return dao.getDashboardsByUser(userId);
	}

	@Override
	public Dashboard findDashboardById(Long id, Integer type) {
		ApplicationContext context = BiUtil.getApplicationContext();
		DashboardDAO dao = (DashboardDAO)context.getBean("dashboardDAO");
		
		return dao.findDashboardById(id, type);
	}

	@Override
	public Boolean storeDashboard(Dashboard dashboard) {
		ApplicationContext context = BiUtil.getApplicationContext();
		DashboardDAO dao = (DashboardDAO)context.getBean("dashboardDAO");
		
		if(dashboard.getId().equals(0L)){
			return createDashboard(dashboard);
		}
		
		Object[] params = null;
		String query = null;
		if(dashboard.getType().equals(Dashboard.DUAL)){
			query = DashboardQueryBuilder.updateDualDashboardQuery();
			params = new Object[]{dashboard.getName(), dashboard.getFirst(), dashboard.getSecond(), dashboard.getId()};
		} else {
			query = DashboardQueryBuilder.updateQuadDashboardQuery();
			params = new Object[]{dashboard.getName(), dashboard.getFirst(), dashboard.getSecond(), dashboard.getThird(), dashboard.getFourth(), dashboard.getId()};
		}
		
		dao.updateDashboard(query, params);
		
		return true;
	}

	
}
